import java.util.List;
import java.util.Scanner;

public class MovieService {
    private Scanner scanner;
    private MovieDAO movieDAO;

    public MovieService(Scanner scanner) {
        this.scanner = scanner;
        this.movieDAO = new MovieDAO();
    }

    // 查看所有电影
    public void listAllMovies() {
        List<Movie> movies = movieDAO.getAllMovies();
        System.out.println("\n=== 电影列表 ===");
        System.out.println("ID\t名称\t\t时长\t类型\t票价");
        System.out.println("----------------------------------------");
        for (Movie movie : movies) {
            System.out.println(String.format("%s\t%s\t%s\t%s\t%.2f",
                    movie.getId(), movie.getName(), movie.getDuration(),
                    movie.getType(), movie.getPrice()));
        }
    }

    // 根据ID查找电影
    public Movie findMovieById(String id) {
        return movieDAO.findMovieById(id);
    }

    // 添加电影
    public boolean addMovie() {
        System.out.print("请输入电影ID: ");
        String id = scanner.nextLine();

        // 检查电影ID是否已存在
        if (movieDAO.findMovieById(id) != null) {
            System.out.println("电影ID已存在！");
            return false;
        }

        System.out.print("请输入电影名称: ");
        String name = scanner.nextLine();
        System.out.print("请输入电影时长(如120min): ");
        String duration = scanner.nextLine();
        System.out.print("请输入电影类型: ");
        String type = scanner.nextLine();
        System.out.print("请输入电影票价: ");
        double price = Double.parseDouble(scanner.nextLine());

        Movie movie = new Movie(id, name, duration, type, price);
        boolean result = movieDAO.addMovie(movie);
        if (result) {
            System.out.println("电影添加成功！");
        } else {
            System.out.println("电影添加失败！");
        }
        return result;
    }

    // 更新电影信息
    public boolean updateMovie() {
        System.out.print("请输入要更新的电影ID: ");
        String id = scanner.nextLine();

        Movie movie = movieDAO.findMovieById(id);
        if (movie == null) {
            System.out.println("电影不存在！");
            return false;
        }

        System.out.print("请输入新的电影名称(" + movie.getName() + "): ");
        String name = scanner.nextLine();
        if (!name.isEmpty()) {
            movie.setName(name);
        }

        System.out.print("请输入新的电影时长(" + movie.getDuration() + "): ");
        String duration = scanner.nextLine();
        if (!duration.isEmpty()) {
            movie.setDuration(duration);
        }

        System.out.print("请输入新的电影类型(" + movie.getType() + "): ");
        String type = scanner.nextLine();
        if (!type.isEmpty()) {
            movie.setType(type);
        }

        System.out.print("请输入新的电影票价(" + movie.getPrice() + "): ");
        String priceStr = scanner.nextLine();
        if (!priceStr.isEmpty()) {
            movie.setPrice(Double.parseDouble(priceStr));
        }

        boolean result = movieDAO.updateMovie(movie);
        if (result) {
            System.out.println("电影更新成功！");
        } else {
            System.out.println("电影更新失败！");
        }
        return result;
    }

    // 删除电影
    public boolean deleteMovie() {
        System.out.print("请输入要删除的电影ID: ");
        String id = scanner.nextLine();

        Movie movie = movieDAO.findMovieById(id);
        if (movie == null) {
            System.out.println("电影不存在！");
            return false;
        }

        boolean result = movieDAO.deleteMovie(id);
        if (result) {
            System.out.println("电影删除成功！");
        } else {
            System.out.println("电影删除失败！");
        }
        return result;
    }
}
